package com.zpl.goods.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.zpl.goods.core.Result;
import com.zpl.goods.entity.Goods;
import com.zpl.goods.entity.dto.GoodKeyWordDTO;
import com.zpl.goods.entity.vo.SellVO;
import com.zpl.goods.service.GoodsService;
import com.zpl.goods.utils.COSStorage;
import io.netty.util.internal.StringUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/goods")
@Api(value = "GoodsController", tags = {"货物管理"})
public class GoodsController {
    @Autowired
    private GoodsService goodsService;

    @Resource
    private COSStorage cosStorage;

    @PostMapping
    @ApiOperation(value = "1.添加货物", notes = "添加", httpMethod = "POST")
    @ApiImplicitParam(name = "goods", value = "货物信息", dataType = "Goods",required = true)
    public Result insert(@RequestBody Goods goods){
        if (goodsService.insert(goods)){
            return Result.success("商品添加成功");
        }
        return Result.fail(goods.getTitle()+"已存在");
    }

    @DeleteMapping("/{id}")
    @ApiOperation(value = "2.删除货物", notes = "通过id删除货物", httpMethod = "DELETE")
    @ApiImplicitParam(name = "id", value = "货物id", dataType = "Long")
    public Result delete(@PathVariable("id") Long id){
        if(goodsService.delete(id)){
            return Result.success("删除成功");
        }
        return Result.fail("删除失败");
    }

    @PutMapping
    @ApiOperation(value = "3.更改货物", notes = "通过id更改货物", httpMethod = "PUT")
    @ApiImplicitParam(name = "goods", value = "货物信息", dataType = "Goods",required = true)
    public Result update(@RequestBody Goods goods) {
        if (goodsService.update(goods)) {
            return Result.success("修改成功");
        }
        return Result.fail("修改失败");
    }

    @GetMapping("/alllist")
    @ApiOperation(value = "4.获取所有货物", notes = "获取所有货物", httpMethod = "GET")
    public Result getAllGoods(){
        List<Goods> goodsList=goodsService.getAllGoods();
        return Result.success(goodsList);
    }

    @GetMapping("/list/{id}")
    @ApiOperation(value = "5.获取货物", notes = "通过id获取货物", httpMethod = "GET")
    @ApiImplicitParam(name = "", value = "", dataType = "String")
    public Result getGood(@PathVariable("id")Long id){
        System.out.println(id);
        Goods goods=goodsService.getGood(id);
        if(goods!=null){
            return Result.success(goods);
        }
        return Result.fail("找不到id");
    }


    @GetMapping("/listfeign/{id}")
    public Goods getGoodFeign(@PathVariable("id")Long id){
        System.out.println(id);
        Goods goods=goodsService.getGood(id);
        System.out.println(goods);
        return goods;
    }


    @GetMapping("/list/title/{title}")
    @ApiOperation(value = "6.获取货物", notes = "通过标题获取货物", httpMethod = "GET")
    @ApiImplicitParam(name = "title", value = "标题", dataType = "String")
    public Result selectByTitle(@PathVariable("title") String title){
        List<Goods> goodsList=goodsService.selectByTitle(title);
        if(goodsList.size()>0){
            return Result.success(goodsList);
        }
        return Result.fail("没有找到相关结果");
    }
    /**
     * 总记录
     * 页大小
     * 当前页
     * @param goodKeyWordDTO
     * @return
     */
//    @PostMapping("/list")
//    @ApiOperation(value = "4.获取所有货物", notes = "获取所有货物", httpMethod = "GET")
//    public Result getMembers(@RequestBody GoodKeyWordDTO goodKeyWordDTO) {
//        IPage<GoodVO> memberIPage=goodsService.getGoodList(goodKeyWordDTO);
//        return Result.success(memberIPage);
//    }

    /**
     * 总记录
     * 页大小
     * 当前页
     * @param goodKeyWordDTO
     * @return
     */
    @PostMapping("/listpage")
    @ApiOperation(value = "7.获取所有货物", notes = "获取所有货物", httpMethod = "POST")
    public Result selectByPage(@RequestBody GoodKeyWordDTO goodKeyWordDTO) {
        IPage<Goods> goodsIPage=goodsService.selectByPage(goodKeyWordDTO);
        return Result.success(goodsIPage);
    }

    @PostMapping("/upload")
    public Result upload(MultipartFile file) throws IOException {
        String path=cosStorage.upload(file.getInputStream());
        if(!StringUtils.isEmpty(path)){
            return Result.success(path);
        }
        return Result.fail("上传失败");
    }

    @DeleteMapping("/file/{path}")
    public Result deleteFile(@PathVariable("path") String path){
        cosStorage.delete(path);
        return Result.success("删除成功!");
    }

    @GetMapping("/sell")
    public Result getSellGoods(){
        List<SellVO> sellVOList=goodsService.getSellGoods();
        if(sellVOList.size()>0){
            return Result.success(sellVOList);
        }
        return Result.fail("查询失败!");
    }

    @GetMapping("/new")
    public Result getNewGoods(){
        List<SellVO> sellVOS=goodsService.getNewGoods();
        if(sellVOS.size()>0){
            return Result.success(sellVOS);
        }
        return Result.fail("查询失败");
    }

    @GetMapping("/like")
    public Result getLike(){
        List<SellVO> vos=goodsService.getLike();
        if(vos.size()>0){
            return Result.success(vos);
        }
        return Result.fail("查询失败!");
    }
}
